package com.yztc.xiami_app.ui.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.adapter.MyAdapter;
import com.yztc.xiami_app.helper.baidumap.ILocationListener;
import com.yztc.xiami_app.helper.baidumap.MapLocation;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 * <p>
 * "分类Fragment"
 */
public class ClassifyFragment extends Fragment implements AdapterView.OnItemClickListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.image_back_classify)
    ImageView imageBackClassify;
    @BindView(R.id.image_clean_classify)
    ImageView imageCleanClassify;
    @BindView(R.id.textView_location)
    TextView textViewLocation;
    @BindView(R.id.listview)
    ListView listview;

    private String[] strs = {"厨房用具", "家居家饰", "家电数码", "食品饮料", "服饰鞋包", "珠宝配饰", "护肤洗护",
            "收藏旅游", "运动汽车"};
    private MyAdapter adapter;
    private TitleFragment myFragment;
    private DetailFragment mDetailFragment;
    public static int mPosition;
    @BindView(R.id.where_in)
    ImageView whereIn;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_btn)
    ImageView searchBtn;
    private Context mContext;
    public static final String TAG = "MyFragment";
    private String str;

    //百度地图定位
    private LocationClient mLocationClient = null;
    private BDLocationListener mBdLocationListener = null;


    private ILocationListener listener = new ILocationListener() {
        @Override
        public void getAddress(String address) {
            Toast.makeText(mContext, address, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void getCity(String city) {
            textViewLocation.setText(city);
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_classify, null);
        mContext = getActivity();
        ButterKnife.bind(this, view);
        initView();

        return view;
    }



    @OnClick({R.id.where_in, R.id.search_btn, R.id.image_back_classify, R.id.image_clean_classify})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.where_in:
                //开启定位
                MapLocation location = new MapLocation(getActivity(), listener);
                location.getAddress(); //获取定位结果，需要监听器接收
                break;
            case R.id.search_btn:
                break;
            case R.id.image_clean_classify:
                searchEt.setText("");
                break;

        }
    }

    private void initView() {
        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //获得当前输入内容
                String number = searchEt.getEditableText().toString();
                if (TextUtils.isEmpty(number)) { //当没有输入内容的时候，清空按钮不可见
                    imageCleanClassify.setVisibility(View.INVISIBLE);
                } else {
                    imageCleanClassify.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        adapter = new MyAdapter(mContext, strs);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);


        //创建MyFragment对象
        myFragment = new TitleFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager()
                .beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, myFragment);
        //通过bundle传值给MyFragment
        Bundle bundle = new Bundle();
        bundle.putString(ClassifyFragment.TAG, strs[mPosition]);
        myFragment.setArguments(bundle);
        fragmentTransaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        //拿到当前位置
        mPosition = position;
        //即使刷新adapter
        adapter.notifyDataSetChanged();
        for (int i = 0; i < strs.length; i++) {
            //myFragment = new TitleFragment();
            mDetailFragment = new DetailFragment();
            FragmentTransaction fragmentTransaction = getFragmentManager()
                    .beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, mDetailFragment);
            Bundle bundle = new Bundle();
            bundle.putString(ClassifyFragment.TAG, "http://m.ttcj.tv/xml/appcategoryjson.htm");
            mDetailFragment.setArguments(bundle);
            fragmentTransaction.commit();

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.unRegisterLocationListener(mBdLocationListener);
            mLocationClient.stop();
        }
    }




}
